﻿package com.prasanna.practice.utils
{	
	[Event(name = "xmlLoaded", type = "com.prasanna.practice.utils.XmlLoaderEvent")] 
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	

	/**
	 * ...
	 * @author Prasanna Patra
	 */

	public class XmlLoader extends EventDispatcher
	{
		
		public var request:URLRequest;
		public var _xmlLoader:URLLoader;
		public var _xml:XML;		

		public function XmlLoader()
		{
			
		}
		public function LoadXML(fileURL:String)
		{
			_xmlLoader = new URLLoader( new URLRequest(fileURL));
			_xmlLoader.addEventListener(Event.COMPLETE, onXMLLoaded);
			_xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLError);			
		}
		
		private function onXMLError(e:IOErrorEvent):void 
		{	
			dispatchEvent(e);
		}

		public function onXMLLoaded(e:Event):void
		{
			_xml = new XML(e.target.data);

			var evt:XmlLoaderEvent = new XmlLoaderEvent(XmlLoaderEvent.XML_LOADED);
			evt.xmlData = _xml;
			dispatchEvent(evt);
		}
	}
}